package com.zg.api.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zg.api.domain.req.ApiServicePageReq;
import com.zg.api.domain.vo.ApiServiceVo;
import com.zg.api.service.ApiServiceService;
import com.zg.api.sys.ApiE;
import com.zg.common.core.dao.IDao;
import com.zg.common.core.dao.apis.ApiServiceDao;
import com.zg.common.core.dao.apis.entity.ApiService;
import com.zg.common.core.service.PageBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>Description: </p>
 *
 * @author zhanfeng.liang
 * @since 2025/6/16 20:11
 */

@Slf4j
@Service
public class ApiServiceServiceImpl extends PageBaseService<ApiService, ApiServiceDao> implements ApiServiceService {


//    @Autowired
//    @Qualifier("apiServiceDao")
//    @Override
//    protected void setDao(IDao<ApiService> dao) {
//        this.dao = dao;
//    }


    @Override
    public Page<ApiServiceVo> pageService(ApiServicePageReq req, Class<ApiServiceVo> voClass) {
        Page<ApiService> page = new Page<>(req.getPage(), req.getPageSize());
        LambdaQueryWrapper<ApiService> qw = buildPageQueryWrapper(req);
        Page<ApiService> pageInfo = baseDao.pageService(page, qw);

        List<ApiServiceVo> voList = pageInfo.getRecords().stream()
                .map(entity -> convertToVo(entity, voClass))
                .collect(Collectors.toList());

        Page<ApiServiceVo> voPage = new Page<>(pageInfo.getCurrent(), pageInfo.getSize(), pageInfo.getTotal());
        voPage.setRecords(voList);

        return voPage;
    }

    @Override
    public  LambdaQueryWrapper<ApiService> buildQueryWrapper(ApiService service){
        LambdaQueryWrapper<ApiService> qw = new LambdaQueryWrapper<>();
        if(service!=null){
            qw.like(!StrUtil.isEmpty(service.getServiceName()), ApiService::getServiceName, service.getServiceName());
            qw.eq(!StrUtil.isEmpty(service.getServiceCode()), ApiService::getServiceCode, service.getServiceCode());
        }


        return qw;
    }

    public List<ApiService> listByName(String name){
        LambdaQueryWrapper<ApiService> qw = new LambdaQueryWrapper<>();
        qw.like(ApiService::getServiceName,name);
        return baseDao.list(qw);
    }

    public ApiService getByCode(String code){
        LambdaQueryWrapper<ApiService> qw = new LambdaQueryWrapper<>();
        qw.eq(ApiService::getServiceCode,code);
        return baseDao.getOne(qw);
    }



    @Override
    protected void preAdd(ApiService source) {
        ApiService project = getByCode(source.getServiceCode());
        if(project!=null){
            ApiE.SERVICE_IS_EXIST.happen();
        }
    }



}
